<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="serverName" v-model="listQuery.filterItem">
      </el-input>
      <el-select clearable style="width: 90px" class="filter-item" v-model="listQuery.importance" placeholder="importance">
        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item">
        </el-option>
      </el-select>
      <el-select @change='handleFilter' style="width: 140px" class="filter-item" v-model="listQuery.sort">
        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key">
        </el-option>
      </el-select>
      <el-button class="filter-item" type="primary"  icon="el-icon-search" @click="handleFilter">search</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">add</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='ID' width="95">
        <template slot-scope="scope">
          {{ 1 + scope.$index + ((listQuery.page -1 ) * listQuery.limit) }}
        </template>
      </el-table-column>
      <el-table-column label="Server">
        <template slot-scope="scope">
          {{scope.row.server_name}}
        </template>
      </el-table-column>
      <el-table-column label="Cpu" width="110" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.cpu}}</span>
        </template>
      </el-table-column>
      <el-table-column label="Memory" width="110" align="center">
        <template slot-scope="scope">
          {{scope.row.memory}}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{scope.row.status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="Hard Disk" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.hard_disk}}</span>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import { getMyApplication } from '@/api/server'

export default {
  name: 'serverTable',
  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        filterItem: null,
        type: null,
        status: null,
        sort: null
      },
      currentRow: 0,
      memorySize: ['8G', '16G', '32G', '64G', '128G'],
      tempServer: {
        serverName: '',
        cpu: '',
        memory: '',
        hard_disk: ''
      },
      importanceOptions: [1, 2, 3],
      serverName: '',
      downloadLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit Server',
        create: 'Create Server'
      },
      rules: {
        server_name: [{ required: true, message: 'server name is required', trigger: 'change' }],
        cpu: [{ required: true, message: 'cpu is required', trigger: 'change' }],
        memory: [{ required: true, message: 'memory is required', trigger: 'blur' }],
        hard_disk: [{ required: true, message: 'hard disk is required', trigger: 'blur' }]
      },
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }]
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getMyApplication(this.listQuery).then(response => {
        this.list = response.dataList
        this.total = response.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleFilter() {
      this.getList()
    },
    handleCreate() {
      this.$router.push({
        name: 'Add Application',
        params: {
        }
      })
    }
  }
}
</script>
